//=========教师类型的数据结构======
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "tlinklist.h"

/*
//链表节点定义
typedef struct tNode
{
	int id;			//工号(遍历) 8位数
	char password[20];//密码(遍历) 20位数
	char name[30];	//姓名(三个char类型存放一个中文字符) 3*10
	char phone[12];	//手机号,[预留一位]存放字符串结束标志
	char state;		//教师状态 0:在职 1:离职
	struct tNode *prev;
	struct tNode *next;
}tNode;
//链表定义
typedef struct
{
	struct tNode *head;
	struct tNode *tail;
}*tLink;
*/

//创建链表
tLink Create_tLink()
{
	tLink TL = (tLink)malloc(sizeof(tLink));
	TL->head = NULL;
	TL->tail = NULL;
	return TL;
}
//放入数据
bool Insert_tLink(tLink TL,int id,char password[],char name[],char phone[],char state)
{
	//查重:若重复,则将其覆盖;若未重复,则插入
	tNode* node = Search_tLink(TL,id);
	if(node == NULL) 
	{
		//未重复
		node = (tNode*)malloc(sizeof(tNode));
		
		node->prev = TL->tail;//从链表尾部插入
		if(TL->head == NULL) TL->head = node;
		else TL->tail->next = node;
		TL->tail = node;
	}
	//若重复,无需变化节点前后端指针

	node->id = id;
	//node->state = state;
	strcpy(node->password,password);
	strcpy(node->name,name);
	//printf("%hhd\n",node->state);
	strcpy(node->phone,phone);
	node->state = state;

	return true;
}
//工号查找,返回该工号的所有信息[注意密码],用于确认密码和查询其他信息
tNode* Search_tLink(tLink TL,int id)
{
	tNode* node = TL->head;
	while(node != NULL)
	{
		if(node->id == id) return node;
		node = node->next;
	}
	return NULL;//若没有则返回空
} 

//初始化链表[已验证]
bool Init_tLink(tLink TL)
{
	//test
	//printf("\nlaoizchihuoguo\n");

	tNode* node = TL->head;
	tNode* tmp = node;
	while(node != NULL)
	{
		node = node->next;
		free(tmp);
		tmp = node;
	}
	TL->head = NULL;
	TL->tail = NULL;
}

//=============以下用于测试=============
//遍历
bool Travel_tLink(tLink TL)
{
	tNode* node = TL->head;
	while(node != NULL)
	{
		printf("%d ",node->id);
		printf("%s ",node->password);
		printf("%s ",node->name);
		printf("%s ",node->phone);
		printf("%hhd ",node->state);
		switch(node->state)
		{
			case 0 : printf("在职\n");break;
			case 1 : printf("离职\n");break;
			default : printf("未知状态\n");break;
		}
		node = node->next;
	}
}
